﻿/*************************************************
网络管理对象
author：Daixiwei
**************************************************/
using System;
using System.Collections.Generic;

namespace taurus.client
{
    /// <summary>
    /// The net manager class
    /// </summary>
    public static class NetManager 
    {
        private static List<TaurusClient> _clientList = new List<TaurusClient>();
        private static Dictionary<int,TaurusClient> _clientMap = new Dictionary<int,TaurusClient>();


        /// <summary>
        /// 连接超时单位(s)
        /// </summary>
        public static int ConnectTimeOut = 10;
        /// <summary>
        /// 读取超时单位(s)
        /// </summary>
        public static int ReadTimeOut = 10;
        /// <summary>
        /// 发送超时单位(s)
        /// </summary>
        public static int WriteTimeOut = 5;
        /// <summary>
        /// 网络版本号
        /// </summary>
        public static int Version = 10;

        public static bool DebugPrint = true;


        internal static void addNetClient(TaurusClient net)
        {
            if (net == null)
            {
                throw new ArgumentException("Controller is null, it can't be added.");
            }
            if (_clientMap.ContainsKey(net.getId()))
            {
                throw new ArgumentException(string.Concat("A controller with id: ", net.getId(), " already exists! Controller can't be added: ", net));
            }
            lock (_clientList)
            {
                _clientMap.Add(net.getId(), net);
                _clientList.Add(net);
            }
            
        }

        /// <summary>
        /// The process events
        /// </summary>
        public static void processEvents()
        {
            for (int i = 0; i < _clientList.Count; ++i)
            {
                TaurusClient nc = _clientList[i];
                if (nc._kill)
                {
                    lock(_clientList)
                    {
                        _clientMap.Remove(nc.getId());
                        _clientList.Remove(nc);
                    }
                    i--;
                    continue;
                }else
                {
                    nc.processEvent();
                }
            }

        }

        /// <summary>
        /// 返回指定的NetClient
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static TaurusClient getNetClient(int id)
        {
            for (int i = 0; i < _clientList.Count; ++i)
            {
                var nc = _clientList[i];
                if (nc.getId() == id) return nc;
            }
            return null;
        }

        /// <summary>
        /// 断开指定连接
        /// </summary>
        /// <param name="id"></param>
        public static void killConnection(int id)
        {
            for (int i = 0; i < _clientList.Count; ++i)
            {
                var nc = _clientList[i];
                if (nc.getId() == id)
                {
                    nc.killConnection();
                    lock (_clientList)
                    {
                        _clientMap.Remove(id);
                        _clientList.Remove(nc);
                    }
                    break;
                }
            }
        }

        /// <summary>
        /// 断开所有连接
        /// </summary>
        public static void killAllConnection()
        {
            lock (_clientList)
            {
                foreach (TaurusClient nc in _clientList)
                {
                    nc.killConnection();
                }
                _clientList.Clear();
                _clientMap.Clear();
            }
        }
    }
}